/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.mavenproject1;

import java.io.InputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Properties;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author janksenhu
 */
public class TestProxy {
    
      public static void main(String[] args) {
        try {
//            URL url = new URL("http://www.baidu.com");
//            // 创建代理服务器
//            InetSocketAddress addr = new InetSocketAddress("localhost",
//                    8085);
//            Proxy proxy_sock = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
//            // 如果我们知道代理server的名字, 可以直接使用
//            // 结束
//            URLConnection conn = url.openConnection(proxy_sock);
//            InputStream in = conn.getInputStream();
//            // InputStream in = url.openStream();
//            String s = IOUtils.toString(in);
//            System.out.println(s);
            
            
//            SocketChannel sc = SocketChannel.open(new InetSocketAddress("127.0.0.1", 1080));  
  
//            Properties prop = System.getProperties();
//              // socks代理服务器的地址与端口
//            prop.setProperty("socksProxyHost", "127.0.0.1");
//            prop.setProperty("socksProxyPort", "1080");
//            // 设置登陆到代理服务器的用户名和密码
//             Authenticator.setDefault(new MyAuthenticator("janksenhu", "hjs312SZC"));
            
            //以下地址是代理服务器的地址  
            Socket socket = new Socket("127.0.0.1", 1080);  
            //写与的内容就是遵循HTTP请求协议格式的内容，请求百度  
            socket.getOutputStream().write(new String("GET http://www.baidu.com/ HTTP/1.1\r\n\r\n").getBytes());  
            byte[] bs = new byte[1024];  
            InputStream is = socket.getInputStream();  
            int i;  
            while ((i = is.read(bs)) > 0) {  
                System.out.println(new String(bs, 0, i));  
            }  
            is.close();  
            
            
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      
      
       static class MyAuthenticator extends Authenticator {
        private String user = "";
        private String password = "";
        public MyAuthenticator(String user, String password) {
            this.user = user;
            this.password = password;
        }
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    }
}
